/**************************************************************************
 *                                                                        *
 *         Copyright (c) 2014 by Generalplus Inc.                         *
 *                                                                        *
 *  This software is copyrighted by and is the property of Generalplus    *
 *  Inc. All rights are reserved by Generalplus Inc.                      *
 *  This software may only be used in accordance with the                 *
 *  corresponding license agreement. Any unauthorized use, duplication,   *
 *  distribution, or disclosure of this software is expressly forbidden.  *
 *                                                                        *
 *  This Copyright notice MUST not be removed or modified without prior   *
 *  written consent of Generalplus Technology Co., Ltd.                   *
 *                                                                        *
 *  Generalplus Inc. reserves the right to modify this software           *
 *  without notice.                                                       *
 *                                                                        *
 *  Generalplus Inc.                                                      *
 *  No.19, Industry E. Rd. IV, Hsinchu Science Park                       *
 *  Hsinchu City 30078, Taiwan, R.O.C.                                    *
 *                                                                        *
 **************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "drv_l1_sfr.h"
#include "drv_l1_csi.h"
#include "drv_l1_i2c.h"
#include "drv_l2_sensor.h"
#include "drv_l2_sccb.h"
#include "drv_l2_cdsp.h"
#include "drv_l2_cdsp_config.h"
#include "gplib_print_string.h"

#if (defined _SENSOR_JXH42_CDSP_DVP) && (_SENSOR_JXH42_CDSP_DVP == 1)

#include "drv_l2_user_calibration.h"

/**************************************************************************
 *                           C O N S T A N T S                            *
 **************************************************************************/
#ifndef DISABLE
#define DISABLE 0
#endif
#ifndef ENABLE
#define ENABLE	1
#endif

#define COLOR_BAR_EN		0
#define SOI_GROUP_WRITE		1		//0:non group write,1:group write, 2:test
#define AEAWB_TASK_ONOFF	ENABLE

#define H42_ID							0x60	//0x64,0x68,0x6C

/*MIPI Rx Input-->ISP Input*/
#define H42_WIDTH						1280
#define H42_HEIGHT						720

/*ISP Output*/
#define H42_OUT_WIDTH					1280
#define H42_OUT_HEIGHT					720

/**************************************************************************
 *                              M A C R O S                               *
 **************************************************************************/

#define JXH42_30FPS_50HZ_DAY_EV_IDX		140
#define JXH42_30FPS_50HZ_NIGHT_EV_IDX	190
#define JXH42_30FPS_50HZ_EXP_TIME_TOTAL 209
#define JXH42_30FPS_50HZ_INIT_EV_IDX	((JXH42_30FPS_50HZ_DAY_EV_IDX + JXH42_30FPS_50HZ_NIGHT_EV_IDX) >> 1)
#define JXH42_30FPS_50HZ_MAX_EV_IDX		(JXH42_30FPS_50HZ_EXP_TIME_TOTAL - 10)
#define JXH42_30FPS_60HZ_DAY_EV_IDX		137
#define JXH42_30FPS_60HZ_NIGHT_EV_IDX	190
#define JXH42_30FPS_60HZ_EXP_TIME_TOTAL 215
#define JXH42_30FPS_60HZ_INIT_EV_IDX	((JXH42_30FPS_60HZ_DAY_EV_IDX + JXH42_30FPS_60HZ_NIGHT_EV_IDX) >> 1)
#define JXH42_30FPS_60HZ_MAX_EV_IDX		(JXH42_30FPS_60HZ_EXP_TIME_TOTAL - 10)
/*
#define H42_24FPS_50HZ_DAY_EV_IDX 			138
#define H42_24FPS_50HZ_NIGHT_EV_IDX			194
#define H42_24FPS_50HZ_EXP_TIME_TOTAL		254
#define H42_24FPS_50HZ_INIT_EV_IDX 			((H42_24FPS_50HZ_DAY_EV_IDX + H42_24FPS_50HZ_NIGHT_EV_IDX) >> 1)
#define H42_24FPS_50HZ_MAX_EV_IDX			 (H42_24FPS_50HZ_EXP_TIME_TOTAL - 10)


#define H42_24FPS_60HZ_DAY_EV_IDX 			135
#define H42_24FPS_60HZ_NIGHT_EV_IDX			195
#define H42_24FPS_60HZ_EXP_TIME_TOTAL		255
#define H42_24FPS_60HZ_INIT_EV_IDX 			((H42_24FPS_60HZ_DAY_EV_IDX + H42_24FPS_60HZ_NIGHT_EV_IDX) >> 1)
#define H42_24FPS_60HZ_MAX_EV_IDX			(H42_24FPS_60HZ_EXP_TIME_TOTAL - 10)
*/

/**************************************************************************
 *                          D A T A    T Y P E S                          *
 **************************************************************************/
 /*
typedef struct regval8_s
{
	INT8U	reg_num;
	INT8U	value;
} regval8_t;
*/

/**************************************************************************
 *                         G L O B A L    D A T A                         *
 **************************************************************************/
static sensor_exposure_t		seInfo;
static int *p_expTime_table;
static int						pre_sensor_a_gain, pre_sensor_time;

#if C_SENSOR_SAVE_RAW_DATA
extern INT32U g_raw_mode_idx;
#endif

#if SENSOR_I2C_MODE == SENSOR_I2C_MODE_SW
sccb_config_t h42_handle =
{
	SENSOR_I2C_SCL,		//scl_port
	SENSOR_I2C_DRIVING,	//scl_drv
	SENSOR_I2C_SDA,		//sda_port
	SENSOR_I2C_DRIVING,	//sda_drv
	0,					//pwdn_port
	0,					//pwdn_drv
	0,					//have_pwdn
	8,					//RegBits
	8,					//DataBits
	H42_ID,				//slaveAddr
	0x20000,			//timeout
	100					//clock_rate
};

#elif SENSOR_I2C_MODE == SENSOR_I2C_MODE_HW
static INT8U read_i2c = 0;

static drv_l1_i2c_bus_handle_t	h42_handle =
{
	SENSOR_I2C_DEV,
	H42_ID,
	100
};
#endif


/***********************************
//INI Create Date : 20150924 by soi eason update
//Terra Ver :
//Create By easonlin

;MCLK:24 MHz
;PCLK:36
;Mipi PCLK:36
;VCO:360
;FrameW:1248(0x508)
;FrameH:728(0x2D8)
************************************/
static const regval8_t	jxh42_720P_30_init_table[] =
{					//new 20150924 by soi eason update
	//;;INI Start
	{ 0x12, 0x40 },

	//DVP Setting
	{ 0x0D, 0x40 },
	{ 0x1F, 0x04 },

	//PLL Setting
	{ 0x0E, 0x1D },
	{ 0x0F, 0x09 },
#if 1				//36M Pclk
	{ 0x10, 0x1E },
#else //12M Pclk
	{ 0x10, 0x0A },
#endif
	{ 0x11, 0x80 },

	//Frame/Window
	{ 0x20, 0x40 },
	{ 0x21, 0x06 },
	{ 0x22, 0xEE },
	{ 0x23, 0x02 },
	{ 0x24, 0x08 },

	//+8
	{ 0x25, 0xD8 },

	//+8
	{ 0x26, 0x25 },
#if SENSOR_FLIP
	{ 0x27, 0x35 },

	//0x33//Hoffset
	{ 0x29, 0x01 },
#else
	//{0x27,0x3b},	//0x49//Hoffset
	{ 0x27, 0x39 },

	//0x39
	{ 0x29, 0x01 },
#endif
	{ 0x28, 0x0D },

	//Voffset
	{ 0x2A, 0x24 },
	{ 0x2B, 0x29 },
	{ 0x2C, 0x04 },
	{ 0x2D, 0x00 },
	{ 0x2E, 0xB9 },
	{ 0x2F, 0x00 },

	//Sensor Timing
	{ 0x30, 0x92 },
	{ 0x31, 0x0A },
	{ 0x32, 0xAA },
	{ 0x33, 0x14 },
	{ 0x34, 0x38 },
	{ 0x35, 0x54 },
	{ 0x42, 0x41 },
	{ 0x43, 0x50 },

	//Interface
	{ 0x1D, 0xFF },
	{ 0x1E, 0x9F },

	//1F},9F
	{ 0x6C, 0x90 },
	{ 0x73, 0xB3 },
	{ 0x70, 0x68 },
	{ 0x76, 0x40 },
	{ 0x77, 0x06 },
	{ 0x71, 0x4A },
	{ 0x72, 0x48 },
	{ 0x6D, 0xA2 },

	//Array/AnADC/PWC
	{ 0x48, 0x40 },
	{ 0x60, 0xA4 },
	{ 0x61, 0xFF },
	{ 0x62, 0x40 },
	{ 0x65, 0x00 },
	{ 0x66, 0x20 },
	{ 0x67, 0x30 },
	{ 0x68, 0x04 },
	{ 0x69, 0x74 },
	{ 0x6F, 0x24 },

	//Black Sun
	{ 0x63, 0x51 },
	{ 0x6A, 0x09 },

	//AE/AG/ABLC
	/*AE:Exposure time, Gain*/
	{ 0x00, 0x00 },
	{ 0x01, 0xD0 },
	{ 0x02, 0x02 },
	{ 0x13, 0x87 },
	{ 0x14, 0x80 },
	{ 0x16, 0xC0 },
	{ 0x17, 0x40 },
	{ 0x18, 0xBB },
	{ 0x19, 0x20 },

	//Gain delay for group write
	{ 0x38, 0x35 },
	{ 0x39, 0x98 },
	{ 0x4a, 0x03 },
	{ 0x49, 0x10 },

	//INI End
#if SENSOR_FLIP
	{ 0x12, 0x30 },
#else
	{ 0x12, 0x00 },
#endif

	//write Done
	{ 0xff, 0xff },
};

static const int jxh42_dvp_30fps_exp_time_gain_50Hz[JXH42_30FPS_50HZ_EXP_TIME_TOTAL][3] =
{					// {time, analog gain, digital gain}
	{ 8,  (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 8,  (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 9,  (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 9,  (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 9,  (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 10, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 10, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 10, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 11, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 11, (int) (1.00 * 16), (int) (1.00 * 64) },
	//10
	{ 11, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 12, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 12, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 13, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 13, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 14, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 14, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 15, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 15, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 16, (int) (1.00 * 16), (int) (1.00 * 64) },
	//20
	{ 16, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 17, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 17, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 18, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 19, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 19, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 20, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 21, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 21, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 22, (int) (1.00 * 16), (int) (1.00 * 64) },
	//30
	{ 23, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 24, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 24, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 25, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 26, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 27, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 28, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 29, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 30, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 31, (int) (1.00 * 16), (int) (1.00 * 64) },
	//40
	{ 32, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 33, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 35, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 36, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 37, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 38, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 40, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 41, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 43, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 44, (int) (1.00 * 16), (int) (1.00 * 64) },
	//50
	{ 46, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 47, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 49, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 51, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 52, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 54, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 56, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 58, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 60, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 62, (int) (1.00 * 16), (int) (1.00 * 64) },
	//60
	{ 65, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 67, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 69, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 72, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 74, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 77, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 80, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 82, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 85, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 88, (int) (1.00 * 16), (int) (1.00 * 64) },
	//70
	{ 91,  (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 95,  (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 98,  (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 101, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 105, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 109, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 112, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 116, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 121, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 125, (int) (1.00 * 16), (int) (1.00 * 64) },
	//80
	{ 129, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 134, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 139, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 143, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 148, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 154, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 159, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 165, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 171, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 177, (int) (1.00 * 16), (int) (1.00 * 64) },
	//90
	{ 183, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 189, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 196, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 203, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 210, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 217, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 225, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 225, (int) (1.04 * 16), (int) (1.00 * 64) },
	{ 225, (int) (1.06 * 16), (int) (1.00 * 64) },
	{ 225, (int) (1.11 * 16), (int) (1.00 * 64) },
	//100
	{ 225, (int) (1.13 * 16), (int) (1.00 * 64) },
	{ 225, (int) (1.19 * 16), (int) (1.00 * 64) },
	{ 225, (int) (1.25 * 16), (int) (1.00 * 64) },
	{ 225, (int) (1.27 * 16), (int) (1.00 * 64) },
	{ 225, (int) (1.31 * 16), (int) (1.00 * 64) },
	{ 225, (int) (1.38 * 16), (int) (1.00 * 64) },
	{ 225, (int) (1.41 * 16), (int) (1.00 * 64) },
	{ 225, (int) (1.44 * 16), (int) (1.00 * 64) },
	{ 225, (int) (1.50 * 16), (int) (1.00 * 64) },
	{ 225, (int) (1.56 * 16), (int) (1.00 * 64) },
	//110
	{ 225, (int) (1.63 * 16), (int) (1.00 * 64) },
	{ 225, (int) (1.69 * 16), (int) (1.00 * 64) },
	{ 225, (int) (1.75 * 16), (int) (1.00 * 64) },
	{ 225, (int) (1.81 * 16), (int) (1.00 * 64) },
	{ 225, (int) (1.88 * 16), (int) (1.00 * 64) },
	{ 225, (int) (1.94 * 16), (int) (1.00 * 64) },
	{ 450, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 450, (int) (1.04 * 16), (int) (1.00 * 64) },
	{ 450, (int) (1.06 * 16), (int) (1.00 * 64) },
	{ 450, (int) (1.11 * 16), (int) (1.00 * 64) },
	//120
	{ 450, (int) (1.13 * 16), (int) (1.00 * 64) },
	{ 450, (int) (1.19 * 16), (int) (1.00 * 64) },
	{ 450, (int) (1.25 * 16), (int) (1.00 * 64) },
	{ 450, (int) (1.27 * 16), (int) (1.00 * 64) },
	{ 450, (int) (1.31 * 16), (int) (1.00 * 64) },
	{ 450, (int) (1.38 * 16), (int) (1.00 * 64) },
	{ 450, (int) (1.41 * 16), (int) (1.00 * 64) },
	{ 450, (int) (1.44 * 16), (int) (1.00 * 64) },
	{ 450, (int) (1.50 * 16), (int) (1.00 * 64) },
	{ 450, (int) (1.56 * 16), (int) (1.00 * 64) },
	//130
	{ 450, (int) (1.63 * 16), (int) (1.00 * 64) },
	{ 450, (int) (1.69 * 16), (int) (1.00 * 64) },
	{ 450, (int) (1.75 * 16), (int) (1.00 * 64) },
	{ 450, (int) (1.81 * 16), (int) (1.00 * 64) },
	{ 450, (int) (1.88 * 16), (int) (1.00 * 64) },
	{ 450, (int) (1.94 * 16), (int) (1.00 * 64) },
	{ 675, (int) (1.31 * 16), (int) (1.00 * 64) },
	{ 675, (int) (1.38 * 16), (int) (1.00 * 64) },
	{ 675, (int) (1.41 * 16), (int) (1.00 * 64) },
	{ 675, (int) (1.44 * 16), (int) (1.00 * 64) },
	//140
	{ 675, (int) (1.50 * 16), (int) (1.00 * 64) },
	{ 675, (int) (1.56 * 16), (int) (1.00 * 64) },
	{ 675, (int) (1.63 * 16), (int) (1.00 * 64) },
	{ 675, (int) (1.69 * 16), (int) (1.00 * 64) },
	{ 675, (int) (1.75 * 16), (int) (1.00 * 64) },
	{ 675, (int) (1.81 * 16), (int) (1.00 * 64) },
	{ 675, (int) (1.88 * 16), (int) (1.00 * 64) },
	{ 675, (int) (1.94 * 16), (int) (1.00 * 64) },
	{ 675, (int) (2.00 * 16), (int) (1.00 * 64) },
	{ 675, (int) (2.07 * 16), (int) (1.00 * 64) },
 	//150
	{ 675, (int) (2.14 * 16), (int) (1.00 * 64) },
	{ 675, (int) (2.22 * 16), (int) (1.00 * 64) },
	{ 675, (int) (2.30 * 16), (int) (1.00 * 64) },
	{ 675, (int) (2.38 * 16), (int) (1.00 * 64) },
	{ 675, (int) (2.46 * 16), (int) (1.00 * 64) },
	{ 675, (int) (2.55 * 16), (int) (1.00 * 64) },
	{ 675, (int) (2.64 * 16), (int) (1.00 * 64) },
	{ 675, (int) (2.73 * 16), (int) (1.00 * 64) },
	{ 675, (int) (2.83 * 16), (int) (1.00 * 64) },
	{ 675, (int) (2.93 * 16), (int) (1.00 * 64) },
	//160
	{ 675, (int) (3.03 * 16), (int) (1.00 * 64) },
	{ 675, (int) (3.14 * 16), (int) (1.00 * 64) },
	{ 675, (int) (3.25 * 16), (int) (1.00 * 64) },
	{ 675, (int) (3.36 * 16), (int) (1.00 * 64) },
	{ 675, (int) (3.48 * 16), (int) (1.00 * 64) },
	{ 675, (int) (3.61 * 16), (int) (1.00 * 64) },
	{ 675, (int) (3.73 * 16), (int) (1.00 * 64) },
	{ 675, (int) (3.86 * 16), (int) (1.00 * 64) },
	{ 675, (int) (4.00 * 16), (int) (1.00 * 64) },
	{ 675, (int) (4.14 * 16), (int) (1.00 * 64) },
	//170
	{ 675, (int) (4.29 * 16), (int) (1.00 * 64) },
	{ 675, (int) (4.44 * 16), (int) (1.00 * 64) },
	{ 675, (int) (4.59 * 16), (int) (1.00 * 64) },
	{ 675, (int) (4.76 * 16), (int) (1.00 * 64) },
	{ 675, (int) (4.92 * 16), (int) (1.00 * 64) },
	{ 675, (int) (5.10 * 16), (int) (1.00 * 64) },
	{ 675, (int) (5.28 * 16), (int) (1.00 * 64) },
	{ 675, (int) (5.46 * 16), (int) (1.00 * 64) },
	{ 675, (int) (5.66 * 16), (int) (1.00 * 64) },
	{ 675, (int) (5.86 * 16), (int) (1.00 * 64) },
  	//180
	{ 675, (int) (6.06 * 16), (int) (1.00 * 64) },
	{ 675, (int) (6.28 * 16), (int) (1.00 * 64) },
	{ 675, (int) (6.50 * 16), (int) (1.00 * 64) },
	{ 675, (int) (6.73 * 16), (int) (1.00 * 64) },
	{ 675, (int) (6.96 * 16), (int) (1.00 * 64) },
	{ 675, (int) (7.21 * 16), (int) (1.00 * 64) },
	{ 675, (int) (7.46 * 16), (int) (1.00 * 64) },
	{ 675, (int) (7.73 * 16), (int) (1.00 * 64) },
	{ 675, (int) (8.00 * 16), (int) (1.00 * 64) },
	//30fps*8x
	{ 675, (int) (8.28 * 16), (int) (1.00 * 64) },
	//190
	{ 675, (int) (8.57 * 16), (int) (1.00 * 64) },
	{ 675, (int) (8.88 * 16), (int) (1.00 * 64) },
	{ 675, (int) (9.19 * 16), (int) (1.00 * 64) },
	{ 675, (int) (9.51 * 16), (int) (1.00 * 64) },
	{ 675, (int) (9.85 * 16), (int) (1.00 * 64) },
	{ 675, (int) (10.20 * 16),(int) (1.00 * 64) },
	{ 675, (int) (10.56 * 16),(int) (1.00 * 64) },
	{ 675, (int) (10.93 * 16),(int) (1.00 * 64) },
	{ 675, (int) (11.31 * 16),(int) (1.00 * 64) },
	{ 675, (int) (11.71 * 16),(int) (1.00 * 64) },
	//200
	{ 675, (int) (12.13 * 16),(int) (1.00 * 64) },
	{ 675, (int) (12.55 * 16),(int) (1.00 * 64) },
	{ 675, (int) (13.00 * 16),(int) (1.00 * 64) },
	{ 675, (int) (13.45 * 16),(int) (1.00 * 64) },
	{ 675, (int) (13.93 * 16),(int) (1.00 * 64) },
	{ 675, (int) (14.42 * 16),(int) (1.00 * 64) },
	{ 675, (int) (14.93 * 16),(int) (1.00 * 64) },
	{ 675, (int) (15.45 * 16),(int) (1.00 * 64) },
	{ 675, (int) (16.00 * 16),(int) (1.00 * 64) }	//209,30fps*16x
};

static const int jxh42_dvp_30fps_exp_time_gain_60Hz[JXH42_30FPS_60HZ_EXP_TIME_TOTAL][3] =
{	// {time, analog gain, digital gain}
	{ 8, (int)  (1.00 * 16), (int) (1.00 * 64) },
	{ 8, (int)  (1.00 * 16), (int) (1.00 * 64) },
	{ 8, (int)  (1.00 * 16), (int) (1.00 * 64) },
	{ 9, (int)  (1.00 * 16), (int) (1.00 * 64) },
	{ 9, (int)  (1.00 * 16), (int) (1.00 * 64) },
	{ 9, (int)  (1.00 * 16), (int) (1.00 * 64) },
	{ 10, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 10, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 10, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 11, (int) (1.00 * 16), (int) (1.00 * 64) },
 	//10
	{ 11, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 11, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 12, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 12, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 13, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 13, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 13, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 14, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 14, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 15, (int) (1.00 * 16), (int) (1.00 * 64) },
	//20
	{ 16, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 16, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 17, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 17, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 18, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 18, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 19, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 20, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 20, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 21, (int) (1.00 * 16), (int) (1.00 * 64) },
	//30
	{ 22, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 23, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 24, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 24, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 25, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 26, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 27, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 28, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 29, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 30, (int) (1.00 * 16), (int) (1.00 * 64) },
	//40
	{ 31, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 32, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 33, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 34, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 36, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 37, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 38, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 40, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 41, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 42, (int) (1.00 * 16), (int) (1.00 * 64) },
	//50
	{ 44, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 45, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 47, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 49, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 50, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 52, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 54, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 56, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 58, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 60, (int) (1.00 * 16), (int) (1.00 * 64) },
 	//60
	{ 62, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 64, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 66, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 69, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 71, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 74, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 76, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 79, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 82, (int) (1.00 * 16), (int) (1.00 * 64) },
	{ 85, (int) (1.00 * 16), (int) (1.00 * 64) },
	//70
	{ 88,  (int) (1.00 * 16),(int) (1.00 * 64) },
	{ 91,  (int) (1.00 * 16),(int) (1.00 * 64) },
	{ 94,  (int) (1.00 * 16),(int) (1.00 * 64) },
	{ 97,  (int) (1.00 * 16),(int) (1.00 * 64) },
	{ 101, (int) (1.00 * 16),(int) (1.00 * 64) },
	{ 104, (int) (1.00 * 16),(int) (1.00 * 64) },
	{ 108, (int) (1.00 * 16),(int) (1.00 * 64) },
	{ 112, (int) (1.00 * 16),(int) (1.00 * 64) },
	{ 116, (int) (1.00 * 16),(int) (1.00 * 64) },
	{ 120, (int) (1.00 * 16),(int) (1.00 * 64) },
	//80
	{ 124, (int) (1.00 * 16),(int) (1.00 * 64) },
	{ 128, (int) (1.00 * 16),(int) (1.00 * 64) },
	{ 133, (int) (1.00 * 16),(int) (1.00 * 64) },
	{ 138, (int) (1.00 * 16),(int) (1.00 * 64) },
	{ 142, (int) (1.00 * 16),(int) (1.00 * 64) },
	{ 148, (int) (1.00 * 16),(int) (1.00 * 64) },
	{ 153, (int) (1.00 * 16),(int) (1.00 * 64) },
	{ 158, (int) (1.00 * 16),(int) (1.00 * 64) },
	{ 164, (int) (1.00 * 16),(int) (1.00 * 64) },
	{ 169, (int) (1.00 * 16),(int) (1.00 * 64) },
	//90
	{ 175, (int) (1.00 * 16),(int) (1.00 * 64) },
	{ 182, (int) (1.00 * 16),(int) (1.00 * 64) },
	{ 188, (int) (1.00 * 16),(int) (1.00 * 64) },
	{ 188, (int) (1.04 * 16),(int) (1.00 * 64) },
	{ 188, (int) (1.06 * 16),(int) (1.00 * 64) },
	{ 188, (int) (1.11 * 16),(int) (1.00 * 64) },
	{ 188, (int) (1.13 * 16),(int) (1.00 * 64) },
	{ 188, (int) (1.19 * 16),(int) (1.00 * 64) },
	{ 188, (int) (1.25 * 16),(int) (1.00 * 64) },
	{ 188, (int) (1.27 * 16),(int) (1.00 * 64) },
	//100
	{ 188, (int) (1.31 * 16),(int) (1.00 * 64) },
	{ 188, (int) (1.38 * 16),(int) (1.00 * 64) },
	{ 188, (int) (1.41 * 16),(int) (1.00 * 64) },
	{ 188, (int) (1.44 * 16),(int) (1.00 * 64) },
	{ 188, (int) (1.50 * 16),(int) (1.00 * 64) },
	{ 188, (int) (1.56 * 16),(int) (1.00 * 64) },
	{ 188, (int) (1.63 * 16),(int) (1.00 * 64) },
	{ 188, (int) (1.69 * 16),(int) (1.00 * 64) },
	{ 188, (int) (1.75 * 16),(int) (1.00 * 64) },
	{ 188, (int) (1.81 * 16),(int) (1.00 * 64) },
	//110
	{ 188, (int) (1.88 * 16),(int) (1.00 * 64) },
	{ 188, (int) (1.94 * 16),(int) (1.00 * 64) },
	{ 375, (int) (1.00 * 16),(int) (1.00 * 64) },
	{ 375, (int) (1.04 * 16),(int) (1.00 * 64) },
	{ 375, (int) (1.06 * 16),(int) (1.00 * 64) },
	{ 375, (int) (1.11 * 16),(int) (1.00 * 64) },
	{ 375, (int) (1.13 * 16),(int) (1.00 * 64) },
	{ 375, (int) (1.19 * 16),(int) (1.00 * 64) },
	{ 375, (int) (1.25 * 16),(int) (1.00 * 64) },
	{ 375, (int) (1.27 * 16),(int) (1.00 * 64) },
	//120
	{ 375, (int) (1.31 * 16),(int) (1.00 * 64) },
	{ 375, (int) (1.38 * 16),(int) (1.00 * 64) },
	{ 375, (int) (1.41 * 16),(int) (1.00 * 64) },
	{ 375, (int) (1.44 * 16),(int) (1.00 * 64) },
	{ 375, (int) (1.50 * 16),(int) (1.00 * 64) },
	{ 375, (int) (1.56 * 16),(int) (1.00 * 64) },
	{ 375, (int) (1.63 * 16),(int) (1.00 * 64) },
	{ 375, (int) (1.69 * 16),(int) (1.00 * 64) },
	{ 375, (int) (1.75 * 16),(int) (1.00 * 64) },
	{ 375, (int) (1.81 * 16),(int) (1.00 * 64) },
	//130
	{ 375, (int) (1.88 * 16),(int) (1.00 * 64) },
	{ 375, (int) (1.94 * 16),(int) (1.00 * 64) },
	{ 563, (int) (1.31 * 16),(int) (1.00 * 64) },
	{ 563, (int) (1.38 * 16),(int) (1.00 * 64) },
	{ 563, (int) (1.41 * 16),(int) (1.00 * 64) },
	{ 563, (int) (1.44 * 16),(int) (1.00 * 64) },
	{ 563, (int) (1.50 * 16),(int) (1.00 * 64) },
	{ 563, (int) (1.56 * 16),(int) (1.00 * 64) },
	{ 563, (int) (1.63 * 16),(int) (1.00 * 64) },
	{ 563, (int) (1.69 * 16),(int) (1.00 * 64) },
	//140
	{ 563, (int) (1.75 * 16),(int) (1.00 * 64) },
	{ 750, (int) (1.31 * 16),(int) (1.00 * 64) },
	{ 750, (int) (1.38 * 16),(int) (1.00 * 64) },
	{ 750, (int) (1.41 * 16),(int) (1.00 * 64) },
	{ 750, (int) (1.44 * 16),(int) (1.00 * 64) },
	{ 750, (int) (1.50 * 16),(int) (1.00 * 64) },
	{ 750, (int) (1.56 * 16),(int) (1.00 * 64) },
	{ 750, (int) (1.63 * 16),(int) (1.00 * 64) },
	{ 750, (int) (1.69 * 16),(int) (1.00 * 64) },
	{ 750, (int) (1.75 * 16),(int) (1.00 * 64) },
 	//150
	{ 750, (int) (1.81 * 16),(int) (1.00 * 64) },
	{ 750, (int) (1.88 * 16),(int) (1.00 * 64) },
	{ 750, (int) (1.94 * 16),(int) (1.00 * 64) },
	{ 750, (int) (2.00 * 16),(int) (1.00 * 64) },
	{ 750, (int) (2.07 * 16),(int) (1.00 * 64) },
	{ 750, (int) (2.14 * 16),(int) (1.00 * 64) },
	{ 750, (int) (2.22 * 16),(int) (1.00 * 64) },
	{ 750, (int) (2.30 * 16),(int) (1.00 * 64) },
	{ 750, (int) (2.38 * 16),(int) (1.00 * 64) },
	{ 750, (int) (2.46 * 16),(int) (1.00 * 64) },
	//160
	{ 750, (int) (2.55 * 16),(int) (1.00 * 64) },
	{ 750, (int) (2.64 * 16),(int) (1.00 * 64) },
	{ 750, (int) (2.73 * 16),(int) (1.00 * 64) },
	{ 750, (int) (2.83 * 16),(int) (1.00 * 64) },
	{ 750, (int) (2.93 * 16),(int) (1.00 * 64) },
	{ 750, (int) (3.03 * 16),(int) (1.00 * 64) },
	{ 750, (int) (3.14 * 16),(int) (1.00 * 64) },
	{ 750, (int) (3.25 * 16),(int) (1.00 * 64) },
	{ 750, (int) (3.36 * 16),(int) (1.00 * 64) },
	{ 750, (int) (3.48 * 16),(int) (1.00 * 64) },
	//170
	{ 750, (int) (3.61 * 16),(int) (1.00 * 64) },
	{ 750, (int) (3.73 * 16),(int) (1.00 * 64) },
	{ 750, (int) (3.86 * 16),(int) (1.00 * 64) },
	{ 750, (int) (4.00 * 16),(int) (1.00 * 64) },
	{ 750, (int) (4.14 * 16),(int) (1.00 * 64) },
	{ 750, (int) (4.29 * 16),(int) (1.00 * 64) },
	{ 750, (int) (4.44 * 16),(int) (1.00 * 64) },
	{ 750, (int) (4.59 * 16),(int) (1.00 * 64) },
	{ 750, (int) (4.76 * 16),(int) (1.00 * 64) },
	{ 750, (int) (4.92 * 16),(int) (1.00 * 64) },
 	//180
	{ 750, (int) (5.10 * 16),(int) (1.00 * 64) },
	{ 750, (int) (5.28 * 16),(int) (1.00 * 64) },
	{ 750, (int) (5.46 * 16),(int) (1.00 * 64) },
	{ 750, (int) (5.66 * 16),(int) (1.00 * 64) },
	{ 750, (int) (5.86 * 16),(int) (1.00 * 64) },
	{ 750, (int) (6.06 * 16),(int) (1.00 * 64) },
	{ 750, (int) (6.28 * 16),(int) (1.00 * 64) },
	{ 750, (int) (6.50 * 16),(int) (1.00 * 64) },
	{ 750, (int) (6.73 * 16),(int) (1.00 * 64) },
	{ 750, (int) (6.96 * 16),(int) (1.00 * 64) },
	//190
	{ 750, (int) (7.21 * 16),(int) (1.00 * 64) },
	//50hz,30fps,8x
	{ 750, (int) (7.46 * 16),(int) (1.00 * 64) },
	{ 750, (int) (7.73 * 16),(int) (1.00 * 64) },
	{ 750, (int) (8.00 * 16),(int) (1.00 * 64) },
	{ 750, (int) (8.28 * 16),(int) (1.00 * 64) },
	{ 750, (int) (8.57 * 16),(int) (1.00 * 64) },
	{ 750, (int) (8.88 * 16),(int) (1.00 * 64) },
	{ 750, (int) (9.19 * 16),(int) (1.00 * 64) },
	{ 750, (int) (9.51 * 16),(int) (1.00 * 64) },
	{ 750, (int) (9.85 * 16),(int) (1.00 * 64) },
	//200
	{ 750, (int) (10.20* 16),(int) (1.00 * 64) },
	{ 750, (int) (10.56* 16),(int) (1.00 * 64) },
	{ 750, (int) (10.93* 16),(int) (1.00 * 64) },
	{ 750, (int) (11.31* 16),(int) (1.00 * 64) },
	{ 750, (int) (11.71* 16),(int) (1.00 * 64) },
	{ 750, (int) (12.13* 16),(int) (1.00 * 64) },
	{ 750, (int) (12.55* 16),(int) (1.00 * 64) },
	{ 750, (int) (13.00* 16),(int) (1.00 * 64) },
	{ 750, (int) (13.45* 16),(int) (1.00 * 64) },
	{ 750, (int) (13.93* 16),(int) (1.00 * 64) },
	//210
	{ 750, (int) (14.42* 16),(int) (1.00 * 64) },
	//50hz,30fps,16x
	{ 750, (int) (14.93* 16),(int) (1.00 * 64) },
	{ 750, (int) (15.45* 16),(int) (1.00 * 64) },
	{ 750, (int) (16.00* 16),(int) (1.00 * 64) },
};

////////////////////////////////////////////////////////////////////////////////////////////////////////

// FUNCTION IMPLEMENTATION
////////////////////////////////////////////////////////////////////////////////////////////////////////
static INT32S h42_sccb_open(void)
{
	#if SENSOR_I2C_MODE == SENSOR_I2C_MODE_HW
	drv_l1_i2c_init(&h42_handle);
	DBG_PRINT("SENSOR_I2C_MODE_HW\r\n");
	#else
	DBG_PRINT("SENSOR_I2C_MODE_SW\r\n");
	#endif

	return STATUS_OK;
}

static void h42_sccb_close(void)
{
	#if SENSOR_I2C_MODE == SENSOR_I2C_MODE_HW
	drv_l1_i2c_uninit(&h42_handle);
	#endif
}

#if SENSOR_I2C_MODE == SENSOR_I2C_MODE_SW
static INT32S h42_sccb_write(INT16U reg, INT8U value)
{
	return drv_l2_sccb_write(&h42_handle, reg, value);
	//return drv_l1_reg_1byte_data_1byte_write(&h42_handle, reg, value);

}
#endif

#if SENSOR_I2C_MODE == SENSOR_I2C_MODE_SW
static INT32S h42_sccb_read(INT16U reg)
{
	INT16U data;
	if(drv_l2_sccb_read(&h42_handle, reg, &data) >= 0)
	{
		DBG_PRINT("[SW] R:0x%02x, D:0x%02x\r\n", reg, data);
		return STATUS_OK;
	}
	else
	{
		DBG_PRINT("i2C read fail!\n");
		return STATUS_FAIL;
	}
}
#endif

#if SENSOR_I2C_MODE == SENSOR_I2C_MODE_HW
static INT32S h42_sccb_read(INT16U reg, INT8U *value)
{
	if(drv_l1_reg_1byte_data_1byte_read(&h42_handle, reg, value) >= 0)
	{
		//DBG_PRINT("0x%02x, 0x%02x\r\n", reg, *value);
		return STATUS_OK;
	}
	else
	{
		DBG_PRINT("H42_sccb_read fail...\r\n");
		return STATUS_FAIL;
	}
}
#endif


static INT32S h42_sccb_write_table(regval8_t *pTable)
{
	#if SENSOR_I2C_MODE == SENSOR_I2C_MODE_HW
	INT8U 		Rd_data;
	INT8S 		transmitcnt = 0;
	regval8_t  	*pTable1;

	pTable1 = pTable;
	#else
	INT32U retry_cnt = 0;
	#endif

	while(1)
	{
		drv_l1_sys_wdt_clear();
		if(pTable->reg_num == 0xFF && pTable->value == 0xFF)
		{
			break;
		}

		//DBG_PRINT("0x%02x, 0x%02x\r\n", pTable->reg_num, pTable->value);
	#if SENSOR_I2C_MODE == SENSOR_I2C_MODE_SW
		if(h42_sccb_write(pTable->reg_num, pTable->value) < 0)
		{
			DBG_PRINT("sccb write fail.\r\n");
			retry_cnt += 1;
			if(retry_cnt >= 10)
				return STATUS_FAIL;
			continue;
		}
		else
		{
			retry_cnt = 0;
		}
			pTable++;
	#else
		transmitcnt = drv_l1_i2c_group_write(&h42_handle,I2C_MISC_2BYTE_REPEAT,(void*)&pTable,1);
		pTable = pTable + transmitcnt;
	#endif
	}

	#if SENSOR_I2C_MODE == SENSOR_I2C_MODE_HW
		pTable =  pTable1;
		while(1)
		{
			if(pTable->reg_num == 0xFF && pTable->value == 0xFF)
				break;

			h42_sccb_read(pTable->reg_num,&Rd_data);
			DBG_PRINT("addr=0x%x w_data = 0x%x r_data=0x%x \r\n",pTable->reg_num,pTable->value,Rd_data);
			pTable++;
		}
	#endif
	//DBG_PRINT("\r\n\r\n");
	return STATUS_OK;
}

#if SENSOR_I2C_MODE == SENSOR_I2C_MODE_SW
static INT32S h42_sccb_read_table(regval8_t *pTable)
{
	while(1) {
		if(pTable->reg_num == 0x00 && pTable->value == 0x00) {
			break;
		}

		if(h42_sccb_read(pTable->reg_num) < 0) {
			DBG_PRINT("sccb read fail.\r\n");
			continue;
		}
		pTable++;
	}
	DBG_PRINT("\r\n\r\n");

	return STATUS_OK;
}
#endif
/*****************************************************************************************
+++++++++++++++++AEC/AGC
*****************************************************************************************/
static int jxh42_cvt_agc_gain(int agc_gain)
{
	INT32U	h42_agc_gain, i;
	INT32U	pow2;

	pow2 = 0;
	i = 5;
	do
	{
		if(agc_gain <= 0x1f)
			break;

		agc_gain >>= 1;
		pow2++;

		i--;
	} while(i != 0);

	agc_gain -= 0x10;

	if(agc_gain < 0)
		agc_gain = 0;
	h42_agc_gain = (pow2 << 4) + agc_gain;

	return h42_agc_gain;
}

//H42 Group Write
static int h42_set_xfps_exposure_time(sensor_exposure_t *si)
{
	INT8U		t1, t2, t3;
	INT32U		idx, temp;
	INT8S		iso_x = 1;

	#if SENSOR_I2C_MODE == SENSOR_I2C_MODE_SW
	INT8S		ret;
	#else
	INT8S		transmitcnt;
	regval8_t	h42_ae_table[8];
	regval8_t	*aetable;
	aetable = (regval8_t*)&h42_ae_table;
	#endif


	//DBG_PRINT("%s:%d\n", __FUNCTION__, __LINE__);
	/*get iso val*/
	iso_x = (drv_l2_CdspGetIso() & 0xF);
	if(iso_x <= 0)
		iso_x = 1;

	si->sensor_ev_idx += si->ae_ev_step;
	if(si->sensor_ev_idx >= si->max_ev_idx)
		si->sensor_ev_idx = si->max_ev_idx;
	if(si->sensor_ev_idx < 0)
		si->sensor_ev_idx = 0;
#if C_SENSOR_SAVE_RAW_DATA
	idx = (g_raw_mode_idx < 0) ? si->sensor_ev_idx * 3 : g_raw_mode_idx * 3;
	DBG_PRINT("%s:%d %d\r\n", __FUNCTION__, __LINE__, g_raw_mode_idx);
#else
	idx = si->sensor_ev_idx * 3;
#endif
	si->time = p_expTime_table[idx];
	si->analog_gain = p_expTime_table[idx + 1];
	si->digital_gain = p_expTime_table[idx + 2];

	//iso set
	si->analog_gain = (si->analog_gain * iso_x);

	//ae debug
#if 0
	DBG_PRINT("\r\n\r\n***** [EV=%d, offset=%d]: time=%d, analog gain=0x%x\r\n", si->sensor_ev_idx, si->ae_ev_step, si->time, si->analog_gain);
#endif

#if 1
	// exposure time
	//if(si->time != pre_sensor_time)
	{
		pre_sensor_time = si->time;
		temp = si->time;

		t1 = (temp & 0xff);
		t2 = (temp >> 8) & 0x00ff;
#if (SOI_GROUP_WRITE == 0)
		ret = h42_sccb_write(0x01, t1);
		if(ret < 0)
			return ret;
		ret = jxh42_sccb_write(0x02, t2);
#elif (SOI_GROUP_WRITE == 1)			//group write
	#if SENSOR_I2C_MODE == SENSOR_I2C_MODE_SW
		ret = h42_sccb_write(0xC0, 0x01);
		if(ret < 0)
			return ret;
		ret = h42_sccb_write(0xC1, t1);
		if(ret < 0)
			return ret;
		ret = h42_sccb_write(0xC2, 0x02);
		if(ret < 0)
			return ret;
		ret = h42_sccb_write(0xC3, t2);

		if(ret < 0)
		{
			DBG_PRINT("ERR: write sensor time = 0x%x!!!\r\n", temp);
			return ret;
		}
	#elif SENSOR_I2C_MODE == SENSOR_I2C_MODE_HW //-----Hw i2c group write


		h42_ae_table[0].reg_num = 0xC0;
		h42_ae_table[0].value = 0x01;
		h42_ae_table[1].reg_num = 0xC1;
		h42_ae_table[1].value = t1;

		h42_ae_table[2].reg_num = 0xC2;
		h42_ae_table[2].value = 0x02;
		h42_ae_table[3].reg_num = 0xC3;
		h42_ae_table[3].value = t2;

	#endif
	 //----


#else //test
		h42_sccb_write(0x01, 0xD0);
		h42_sccb_write(0x02, 0x02);
		#if SENSOR_I2C_MODE == SENSOR_I2C_MODE_SW
		h42_sccb_read(0x01);
		h42_sccb_read(0x02);
		#endif
#endif

	}

	//gain
	//if(si->analog_gain != pre_sensor_a_gain)
	{
		// gain
		pre_sensor_a_gain = si->analog_gain;

		temp = si->analog_gain;
		temp = jxh42_cvt_agc_gain(temp);
		t3 = temp & 0x007f;
#if (SOI_GROUP_WRITE == 0)
		ret = h42_sccb_write(0x00, t1);
#elif (SOI_GROUP_WRITE == 1)			//group write
	#if SENSOR_I2C_MODE == SENSOR_I2C_MODE_SW
		ret = h42_sccb_write(0xC4, 0x00);
		if(ret < 0)
			return ret;
		ret = h42_sccb_write(0xC5, t3);

		if(ret < 0)
		{
			DBG_PRINT("ERR: write sensor gain = 0x%x, 0x%x, 0x%x !!!\r\n", t1, temp, si->analog_gain);
			return ret;
		}

	#elif SENSOR_I2C_MODE == SENSOR_I2C_MODE_HW //-----hw i2c group write

		h42_ae_table[4].reg_num = 0xC4;
		h42_ae_table[4].value = 0x00;
		h42_ae_table[5].reg_num = 0xC5;
		h42_ae_table[5].value = t3;
	#endif
//DBG_PRINT("*** T=%d%d, G=0x%x\r\n",t2,t1,t3);
#else //test
		/*
		ret = h42_sccb_write(0xC4, 0x00);
		if(ret < 0)
			return ret;
		ret = h42_sccb_write(0xC5, 0x26);
		*/
		h42_sccb_write(0x00, 0x0a);
		#if SENSOR_I2C_MODE == SENSOR_I2C_MODE_SW
		h42_sccb_read(0x00);
		#endif
#endif

	}

#if (SOI_GROUP_WRITE == 1)				//group write
	#if SENSOR_I2C_MODE == SENSOR_I2C_MODE_SW
		#if SENSOR_FLIP
		ret = h42_sccb_write(0x12, 0x38);	//group write enable
		#else
		ret = h42_sccb_write(0x12, 0x08);	//group write enable
		#endif
		if(ret < 0)
		{
			DBG_PRINT("ERR: triger group write FAIL!!!\r\n");
			return ret;
		}
	#elif SENSOR_I2C_MODE == SENSOR_I2C_MODE_HW

		#if SENSOR_FLIP
		h42_ae_table[6].reg_num = 0x12;
		h42_ae_table[6].value = 0x38;	//group write enable
		#else
		h42_ae_table[6].reg_num = 0x12;
		h42_ae_table[6].value = 0x08;	//group write enable
		#endif

		//end
		h42_ae_table[7].reg_num = 0xff;
		h42_ae_table[7].value = 0xff;

		//DBG_PRINT(" >");
		transmitcnt = drv_l1_i2c_group_write(&h42_handle,I2C_MISC_2BYTE_REPEAT,(void*)&aetable,1);

		if(read_i2c)//  SENSOR_I2C_MODE == SENSOR_I2C_MODE_HW
		{
			INT8U Rd_data;
			aetable = (regval8_t*)&h42_ae_table;

			while(1) {
				if(aetable->reg_num == 0xFF && aetable->value == 0xFF)
				   break;

				h42_sccb_read(aetable->reg_num,&Rd_data);
				DBG_PRINT("addr=0x%x w_data = 0x%x r_data=0x%x \r\n",aetable->reg_num,aetable->value,Rd_data);
				aetable++;
			}
		}

	#endif
#endif

#endif
	return 0;
}

static void h42_set_ae(int ev_step)
{
	seInfo.ae_ev_step = ev_step;
	h42_set_xfps_exposure_time(&seInfo);
}

void sensor_register_ae_ctrl(INT32U *handle)
{
	*handle = (INT32U) h42_set_ae;
}

void sensor_get_ae_info(sensor_exposure_t *si)
{
	gp_memcpy((INT8S*)si, (INT8S*)&seInfo, sizeof(sensor_exposure_t));
}

void sensor_set_max_lum(int max_lum)
{
	seInfo.max_ev_idx = seInfo.total_ev_idx - (128 - max_lum);
	DBG_PRINT("MaxLum:%d, MaxEvIdx:%d, TotalEvIdx:%d\r\n",max_lum, seInfo.max_ev_idx, seInfo.total_ev_idx);
}

/*++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void h42_dvp_set_exp_freq(INT8U freq)
{
	if(freq == 50)
	{
		seInfo.sensor_ev_idx = JXH42_30FPS_50HZ_INIT_EV_IDX;
		seInfo.ae_ev_step = 0;
		seInfo.daylight_ev_idx = JXH42_30FPS_50HZ_DAY_EV_IDX;
		seInfo.night_ev_idx = JXH42_30FPS_50HZ_NIGHT_EV_IDX;
		seInfo.max_ev_idx = JXH42_30FPS_50HZ_MAX_EV_IDX;
		seInfo.total_ev_idx = JXH42_30FPS_50HZ_EXP_TIME_TOTAL;

		p_expTime_table = (int *) jxh42_dvp_30fps_exp_time_gain_50Hz;

		DBG_PRINT(">>Set frequence 50Hz.\r\n");
	}
	else
	if(freq == 60)
	{
		seInfo.sensor_ev_idx = JXH42_30FPS_60HZ_INIT_EV_IDX;
		seInfo.ae_ev_step = 0;
		seInfo.daylight_ev_idx = JXH42_30FPS_60HZ_DAY_EV_IDX;
		seInfo.night_ev_idx = JXH42_30FPS_60HZ_NIGHT_EV_IDX;
		seInfo.max_ev_idx = JXH42_30FPS_60HZ_MAX_EV_IDX;
		seInfo.total_ev_idx = JXH42_30FPS_60HZ_EXP_TIME_TOTAL;

		p_expTime_table = (int *) jxh42_dvp_30fps_exp_time_gain_60Hz;
		DBG_PRINT(">>Set frequence 60Hz.\r\n");
	}
}

void h42_dvp_seinfo_init(void)
{
	seInfo.sensor_ev_idx = JXH42_30FPS_50HZ_INIT_EV_IDX;
	seInfo.ae_ev_step = 0;
	seInfo.daylight_ev_idx = JXH42_30FPS_50HZ_DAY_EV_IDX;
	seInfo.night_ev_idx = JXH42_30FPS_50HZ_NIGHT_EV_IDX;
	seInfo.max_ev_idx = JXH42_30FPS_50HZ_MAX_EV_IDX;
	seInfo.total_ev_idx = JXH42_30FPS_50HZ_EXP_TIME_TOTAL;

	p_expTime_table = (int *) jxh42_dvp_30fps_exp_time_gain_50Hz;

	pre_sensor_time = 1;
	pre_sensor_a_gain = 0x00;

	seInfo.time = 1;
	seInfo.analog_gain = 0x00;
	seInfo.digital_gain = 0x20;
}



/*****************************************************************************************/
static void h42_dvp_sensor_init(void)
{
	DBG_PRINT("%s\r\n", __func__);
	// reguest sccb
	h42_sccb_open();

	// reset sensor
	//jxh42_sccb_write_table((regval8_t *) jxh42_reset_table);

	OSTimeDly(2);	//wait 20ms

	// init sensor
	h42_sccb_write_table((regval8_t *) jxh42_720P_30_init_table);

	#if SENSOR_I2C_MODE == SENSOR_I2C_MODE_SW
	h42_sccb_read_table((regval8_t *)jxh42_720P_30_init_table);
	#endif

	DBG_PRINT("Sensor H22 dvp init completed\r\n");
}

/**
 * @brief   initialization function
 * @param   sensor format parameters
 * @return 	none
 */

//static void jxh42_cdsp_dvp_init(void)
void h42_cdsp_dvp_init(void)
{
	DBG_PRINT("%s\r\n", __func__);

	//ae init
	h42_dvp_seinfo_init();

	//cdsp init
	drv_l2_cdsp_open();


	DBG_PRINT("Sensor jxh42 dvp init completed\r\n");
}

/**
 * @brief   un-initialization function
 * @param   sensor format parameters
 * @return 	none
 */
static void h42_cdsp_dvp_uninit(void)
{
	DBG_PRINT("%s\r\n", __func__);

	// disable mclk
	drv_l2_sensor_set_mclkout(MCLK_NONE, MASTER_CLOCK_MIPI_OFF, MASTER_CLOCK_CSI_OFF);

	// cdsp disable
	drv_l2_cdsp_close();
	// release sccb
	h42_sccb_close();
}

/**
 * @brief   stream start function
 * @param   info index
 *
 * @return 	none
 */
void h42_cdsp_dvp_stream_on(INT32U index, INT32U bufA, INT32U bufB)
{
	INT16U		target_w, target_h, sensor_w, sensor_h;
	gpCdspFmt_t format;

	// set sensor size
	DBG_PRINT("%s = %d\r\n", __func__, index);

	/*Enable Mclk out*/
	drv_l1_csi_set_clock(h42_cdsp_dvp_ops.info[index].mclk, MASTER_CLOCK_MIPI_ON, MASTER_CLOCK_CSI_KEEP);	//unfix u2



	OSTimeDly(1);	//wait 10ms

	// set cdsp format
/*Init Sensor
* Set ISP macro Table need PCLK
*
**/
	//Enable MCLK & Init Sensor
	//jxh42_dvp_sensor_init();


	format.image_source = C_CDSP_DVP;
	format.input_format = h42_cdsp_dvp_ops.info[index].input_format;
	format.output_format = h42_cdsp_dvp_ops.info[index].output_format;
	target_w = h42_cdsp_dvp_ops.info[index].target_w;
	target_h = h42_cdsp_dvp_ops.info[index].target_h;
	format.hpixel = sensor_w = h42_cdsp_dvp_ops.info[index].sensor_w;
	format.vline = sensor_h = h42_cdsp_dvp_ops.info[index].sensor_h;
	format.hoffset = h42_cdsp_dvp_ops.info[index].hoffset;
	format.voffset = h42_cdsp_dvp_ops.info[index].voffset;
	format.sensor_timing_mode = h42_cdsp_dvp_ops.info[index].interface_mode;
	format.sensor_hsync_mode = h42_cdsp_dvp_ops.info[index].hsync_active;
	format.sensor_vsync_mode = h42_cdsp_dvp_ops.info[index].vsync_active;

	if(drv_l2_cdsp_set_fmt(&format) < 0)
	{
		DBG_PRINT("cdsp set fmt err!!!\r\n");
	}

	// set scale down
	if((format.hpixel > target_w) || (format.vline > target_h))
	{
		drv_l2_cdsp_set_yuv_scale(target_w, target_h);
	}

	//Enable MCLK & Init Sensor
	h42_dvp_sensor_init();

	// cdsp start
	drv_l2_CdspTableRegister((gpCisCali_t *) &g_cali);
	drv_l2_cdsp_stream_on(AEAWB_TASK_ONOFF, bufA, bufB);		//AE,AWB,SetSensor Task

	drv_l2_cdsp_enable((gpUserFav_t*)&g_FavTable ,sensor_w, sensor_h, target_w, target_h);
	drv_l2_cdsp_sat_contrast_reinit_thr();

#if 1
	// reset sensor ev idx
	seInfo.ae_ev_step = 0;
	h42_set_xfps_exposure_time(&seInfo);
	DBG_PRINT("jxh42 exposure time set!!\r\n");
#endif

	drv_l1_CdspSetDataSource(FRONT_INPUT);
	DBG_PRINT("jxh42 cdsp dvp setting done!!\r\n");


	/*Dump cdsp register value for debug*/
	#if 0
	{
		INT32U sensorBufferSize, sensorBuffer;

		sensorBufferSize = 1280*720*2;//SENSOR_SRC_WIDTH*SENSOR_SRC_HEIGHT*2;
		sensorBuffer = (INT32U) gp_malloc_align(sensorBufferSize, 32);
		gp_memset((INT8S*) sensorBuffer, 0, (1280*720*2));

		DBG_PRINT("sensorBuffer:0x%x \r\n",sensorBuffer);
		save_raw10(sensorBuffer, MODE_PURERAW, FRONT_INPUT);


		/*Dump cdsp register value for debug*/
		//gp_CdspDumpRegTable();
		//gp_MipiRxDumpRegTable();
		while(1);
		//{
		//	DBG_PRINT("*");
		//};
	}
	#endif
}

/**
 * @brief   stream stop function
 * @param   none
 * @return 	none
 */
static void h42_cdsp_dvp_stream_off(void)
{
	DBG_PRINT("%s:no function \r\n", __func__);

	//drv_l2_cdsp_stream_off();
}

/**
 * @brief   get info function
 * @param   none
 * @return 	pointer to sensor information data
 */
static drv_l2_sensor_info_t *h42_cdsp_dvp_get_info(INT32U index)
{
	if(index > (MAX_INFO_NUM - 1))
	{
		return NULL;
	}
	else
	{
		return (drv_l2_sensor_info_t *) &h42_cdsp_dvp_ops.info[index];
	}
}

/*********************************************
*	sensor ops declaration
*********************************************/
const drv_l2_sensor_ops_t h42_cdsp_dvp_ops =
{
	SENSOR_SOURCE_DVP,
	SENSOR_PATH_CDSP,
	h42_cdsp_dvp_init,
	h42_cdsp_dvp_uninit,
	h42_cdsp_dvp_stream_on,
	h42_cdsp_dvp_stream_off,
	NULL,
	NULL,
	h42_dvp_set_exp_freq,
	h42_cdsp_dvp_get_info,
	{
		/* 1st info */
		{
			#if 0 // for FPGA test
			MASTER_CLOCK_27MHZ,
			#else
			MASTER_CLOCK_24MHZ,
			#endif
			V4L2_PIX_FMT_SBGGR10,	/* input format */
			V4L2_PIX_FMT_YUYV,		/* output format */
			30,						/* FPS in sensor */
			H42_OUT_WIDTH,			/* target width */
			H42_OUT_HEIGHT,			/* target height */
			H42_WIDTH,				/* sensor width */
			H42_HEIGHT,				/* sensor height */

			#if SENSOR_FLIP
			5,						/* sensor h offset */
			2,						/* sensor v offset */
			#else
			4,
			3,
			#endif

			MODE_CCIR_601,			/* input interface */
			MODE_NONE_INTERLACE,	/* interlace mode */
			MODE_ACTIVE_HIGH,		/* hsync pin active level */
			MODE_ACTIVE_HIGH,		/* vsync pin active level */
		}
	}
};
#endif //(defined _SENSOR_JXH42_CDSP_DVP) && (_SENSOR_JXH42_CDSP_DVP == 1)
